package logic

import (
	"context"
	"fmt"
	"gorm.io/gorm"
	"person/models/mysql"
	"person/personrpc/internal/svc"
	"person/personrpc/personrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type PerfectUserLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewPerfectUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PerfectUserLogic {
	return &PerfectUserLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *PerfectUserLogic) PerfectUser(in *personrpc.PerfectUserRequest) (*personrpc.PerfectUserResponse, error) {
	// todo: add your logic here and delete this line
	var FindPerson mysql.Person
	mysql.Db.Debug().Where("username = ?", in.Username).Find(&FindPerson)
	if FindPerson.ID == 0 {
		return nil, fmt.Errorf("用户登录不存在")
	}
	mysql.Db.Debug().Where("id = ?", in.Id).Find(&FindPerson)
	if FindPerson.ID == 0 {
		return nil, fmt.Errorf("完善个人信息不存在")
	}
	person := mysql.Person{
		Model:       gorm.Model{ID: uint(in.Id)},
		Name:        in.Name,
		Marry:       in.Marry,
		Avatar:      in.Avatar,
		Certificate: in.Certificate,
		Gender:      in.Gender,
		DateBirth:   in.DateBirth,
		Mobile:      in.Mobile,
	}
	mysql.Db.Debug().Updates(&person)

	return &personrpc.PerfectUserResponse{Id: int64(person.ID)}, nil
}
